import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Card} from 'antd';
import {FORM_OPTIONS} from '../constants';
import TextInput from 'Shared/components/TextInput';
import {hasPermissions} from './utils';
import {PERMISSIONS} from './constants';
import routes from './routes';
import WrappedSelect from '../common/WrappedSelect';
import {baseDataStatus} from '../Enum';
import {injectIntl, language} from './intl';
const baseDataStatusToList = baseDataStatus.toList(language);

const FormItem = Form.Item;
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    };
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    };
    render() {
        const {formatMessage} = this.props.intl;
        return (
            <Form className="form-standard">
                <Card>
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.name',
                                    defaultMessage: '名称'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="name"
                                    value={this.props.conditions.name}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.dealerCode',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="dealerCode"
                                    value={this.props.conditions.dealerCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="status"
                                    options={baseDataStatusToList}
                                    value={this.props.conditions.status}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                                {formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onResetClick}>
                                {formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            {hasPermissions(this.props.permission, PERMISSIONS.add) &&
                            <Button key="add" type="primary" href={`#${routes.add.url()}`}>{this.props.intl.formatMessage({
                                id: 'queryPanel.operation.add',
                                defaultMessage: '新增'
                            })}</Button> }
                            {hasPermissions(this.props.permission, PERMISSIONS.import) &&
                            <Button key={PERMISSIONS.import} href={`#${routes.import.url()}`}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.operation.import',
                                    defaultMessage: '导入'
                                })}
                            </Button>}
                        </Col>
                    </Row>
                </Card>
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permission: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    conditions: getConditions(state),
    permission: getPermissions(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel())
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
